﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media;

using Socium.Domain.Collections;

namespace Socium.Testings.Sociometric {
	class ColorGenerator {
		public ColorGenerator() {
			_brushes = new LinkedList<Color>(_all_colors.Value);
		}

		readonly Random _random = new Random();
		LinkedList<Color> _brushes; 

		public Color Next() {
			if (_brushes.Count < 2) {
				_brushes = new LinkedList<Color>(_all_colors.Value);
			}

			var index = _random.Next(_brushes.Count);
			for (var node = _brushes.First; node != null; node = node.Next) {
				if (index == 0) {
					var result = node.Value;
					_brushes.Remove(node);

					return result;
				}

				index--;
			}

			return Colors.Chocolate;
		}

		static IEnumerable<Color> GetColors() {
			var type = typeof(Colors);
			var color_type = typeof(Color);
			var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static).FilterLazy(p => color_type.IsAssignableFrom(p.PropertyType));
			return properties.Map(p => (Color)p.GetValue(null, null));
		}

		static readonly Lazy<IEnumerable<Color>> _all_colors = new Lazy<IEnumerable<Color>>(GetColors);
	}
}
